#include "boot_screen.h"
#include "smart_home_ui.h"  // For load_user_settings() function
#include "light_control.h"    // For set_light_state() and set_global_led_state() functions
#include "led_control.h"      // For led_set_state() function
#include "myfont.h"
#include <stdio.h>

// 全局变量
static lv_obj_t *boot_screen = NULL;
static lv_obj_t *welcome_label = NULL;
static lv_obj_t *home_icon = NULL;
static lv_obj_t *progress_arc = NULL;
static lv_obj_t *loading_label = NULL;

// 动画相关
static lv_timer_t *boot_timer = NULL;
static lv_timer_t *progress_timer = NULL;
static int boot_progress = 0;
static boot_complete_cb_t boot_complete_callback = NULL;

// 动画回调函数声明
static void welcome_fade_in_cb(lv_anim_t *a);
static void icon_scale_cb(lv_anim_t *a);
static void progress_update_cb(lv_timer_t *timer);
static void boot_complete_cb(lv_timer_t *timer);

// 自定义动画执行回调
static void zoom_anim_cb(void *obj, int32_t v) {
    lv_obj_set_style_transform_zoom((lv_obj_t*)obj, v, LV_PART_MAIN);
}

static void opa_anim_cb(void *obj, int32_t v) {
    lv_obj_set_style_opa((lv_obj_t*)obj, v, LV_PART_MAIN);
}

// 设置开机完成回调
void set_boot_complete_callback(boot_complete_cb_t callback) {
    boot_complete_callback = callback;
}

// 欢迎文字淡入动画完成回调
static void welcome_fade_in_cb(lv_anim_t *a) {
    // 启动图标缩放动画
    if(home_icon && lv_obj_is_valid(home_icon)) {
        lv_anim_t scale_anim;
        lv_anim_init(&scale_anim);
        lv_anim_set_var(&scale_anim, home_icon);
        lv_anim_set_values(&scale_anim, 50, 100);  // 从50%缩放到100%
        lv_anim_set_time(&scale_anim, 800);
        lv_anim_set_exec_cb(&scale_anim, zoom_anim_cb);
        lv_anim_set_path_cb(&scale_anim, lv_anim_path_bounce);
        lv_anim_set_ready_cb(&scale_anim, icon_scale_cb);
        lv_anim_start(&scale_anim);
    }
}

// 图标缩放动画完成回调
static void icon_scale_cb(lv_anim_t *a) {
    // 启动进度条动画
    if(progress_arc && lv_obj_is_valid(progress_arc) && 
       loading_label && lv_obj_is_valid(loading_label)) {
        lv_obj_clear_flag(progress_arc, LV_OBJ_FLAG_HIDDEN);
        lv_obj_clear_flag(loading_label, LV_OBJ_FLAG_HIDDEN);
        
        // 启动进度更新定时器
        progress_timer = lv_timer_create(progress_update_cb, 50, NULL);
    }
}

// 进度更新回调
static void progress_update_cb(lv_timer_t *timer) {
    if(!progress_arc || !lv_obj_is_valid(progress_arc) ||
       !loading_label || !lv_obj_is_valid(loading_label)) {
        if(timer) lv_timer_del(timer);
        return;
    }
    
    boot_progress += 2;  // 每次增加2%
    
    if(boot_progress <= 100) {
        // 更新进度条
        lv_arc_set_value(progress_arc, boot_progress);
        
        // 更新加载文字
        char loading_text[32];
        if(boot_progress < 30) {
            snprintf(loading_text, sizeof(loading_text), "初始化系统... %d%%", boot_progress);
        } else if(boot_progress < 60) {
            snprintf(loading_text, sizeof(loading_text), "加载智能设备... %d%%", boot_progress);
        } else if(boot_progress < 90) {
            snprintf(loading_text, sizeof(loading_text), "连接家居网络... %d%%", boot_progress);
        } else {
            snprintf(loading_text, sizeof(loading_text), "启动完成... %d%%", boot_progress);
        }
        lv_label_set_text(loading_label, loading_text);
    } else {
        // 进度完成，停止定时器
        lv_timer_del(timer);
        progress_timer = NULL;
        
        // 延迟500ms后进入主界面
        boot_timer = lv_timer_create(boot_complete_cb, 500, NULL);
    }
}

// 开机完成回调
static void boot_complete_cb(lv_timer_t *timer) {
    lv_timer_del(timer);
    boot_timer = NULL;
    
    // 加载用户设置，恢复关机前状态
    load_user_settings();

    // 将恢复的灯光状态应用到硬件LED
    led_set_state(LED1_GPIO, global_light1_state);  // 客厅灯
    led_set_state(LED2_GPIO, global_light2_state);  // 卧室灯
    led_set_state(LED3_GPIO, global_light3_state);   // 厨房灯
    led_set_state(LED4_GPIO, global_light4_state);   // 阳台灯

    // 同步更新灯光控制界面的UI状态
    set_light_state(0, global_light1_state);  // 客厅灯
    set_light_state(1, global_light2_state);  // 卧室灯
    set_light_state(2, global_light3_state);   // 厨房灯
    set_light_state(3, global_light4_state);   // 阳台灯

    // 直接设置全局LED状态数组
    set_global_led_state(0, global_light1_state);  // 客厅灯
    set_global_led_state(1, global_light2_state);  // 卧室灯
    set_global_led_state(2, global_light3_state);   // 厨房灯
    set_global_led_state(3, global_light4_state);   // 阳台灯

    // 清理开机界面
    cleanup_boot_screen();

    // 调用开机完成回调
    if(boot_complete_callback) {
        boot_complete_callback();
    }
}

// 创建开机界面
void create_boot_screen(void) {
    // 重置进度
    boot_progress = 0;
    
    // 创建开机屏幕
    boot_screen = lv_obj_create(NULL);
    lv_obj_set_style_bg_color(boot_screen, lv_color_hex(0x1a1a2e), LV_PART_MAIN);  // 深蓝色背景
    lv_obj_set_style_bg_opa(boot_screen, LV_OPA_COVER, LV_PART_MAIN);
    lv_obj_clear_flag(boot_screen, LV_OBJ_FLAG_SCROLLABLE);
    
    // 加载屏幕
    lv_scr_load_anim(boot_screen, LV_SCR_LOAD_ANIM_FADE_ON, 300, 0, false);
    
    // 创建欢迎文字（初始透明）
    welcome_label = lv_label_create(boot_screen);
    lv_label_set_text(welcome_label, "总裁,欢迎回家!");
    lv_obj_set_style_text_font(welcome_label, &lv_font_source_han_sans_bold_20, LV_PART_MAIN);
    lv_obj_set_style_text_color(welcome_label, lv_color_hex(0xFFFFFF), LV_PART_MAIN);
    lv_obj_align(welcome_label, LV_ALIGN_CENTER, 0, -80);
    lv_obj_set_style_opa(welcome_label, LV_OPA_TRANSP, LV_PART_MAIN);  // 初始透明
    
    // 创建家居图标（使用Unicode房屋符号）
    home_icon = lv_label_create(boot_screen);
    lv_label_set_text(home_icon, LV_SYMBOL_HOME);
    lv_obj_set_style_text_font(home_icon, &lv_font_montserrat_20, LV_PART_MAIN);
    lv_obj_set_style_text_color(home_icon, lv_color_hex(0x3498db), LV_PART_MAIN);  // 蓝色
    lv_obj_align(home_icon, LV_ALIGN_CENTER, 0, -20);
    lv_obj_set_style_transform_zoom(home_icon, 50, LV_PART_MAIN);  // 初始50%大小
    
    // 创建进度圆环（初始隐藏）
    progress_arc = lv_arc_create(boot_screen);
    lv_obj_set_size(progress_arc, 120, 120);
    lv_obj_align(progress_arc, LV_ALIGN_CENTER, 0, 60);
    lv_arc_set_range(progress_arc, 0, 100);
    lv_arc_set_value(progress_arc, 0);
    lv_arc_set_bg_angles(progress_arc, 0, 360);
    lv_obj_set_style_arc_width(progress_arc, 8, LV_PART_MAIN);
    lv_obj_set_style_arc_color(progress_arc, lv_color_hex(0x34495e), LV_PART_MAIN);
    lv_obj_set_style_arc_width(progress_arc, 8, LV_PART_INDICATOR);
    lv_obj_set_style_arc_color(progress_arc, lv_color_hex(0x3498db), LV_PART_INDICATOR);
    lv_obj_add_flag(progress_arc, LV_OBJ_FLAG_HIDDEN);  // 初始隐藏
    
    // 移除进度圆环的旋钮
    lv_obj_set_style_bg_opa(progress_arc, LV_OPA_TRANSP, LV_PART_KNOB);
    lv_obj_set_style_pad_all(progress_arc, 0, LV_PART_KNOB);
    
    // 禁用用户拖动交互
    lv_obj_clear_flag(progress_arc, LV_OBJ_FLAG_CLICKABLE);
    
    // 创建加载文字（初始隐藏）
    loading_label = lv_label_create(boot_screen);
    lv_label_set_text(loading_label, "初始化系统... 0%");
    lv_obj_set_style_text_font(loading_label, &lv_font_source_han_sans_bold_20, LV_PART_MAIN);
    lv_obj_set_style_text_color(loading_label, lv_color_hex(0xBDC3C7), LV_PART_MAIN);  // 浅灰色
    lv_obj_align(loading_label, LV_ALIGN_CENTER, 0, 140);
    lv_obj_add_flag(loading_label, LV_OBJ_FLAG_HIDDEN);  // 初始隐藏
    
    // 启动欢迎文字淡入动画
    lv_anim_t fade_anim;
    lv_anim_init(&fade_anim);
    lv_anim_set_var(&fade_anim, welcome_label);
    lv_anim_set_values(&fade_anim, LV_OPA_TRANSP, LV_OPA_COVER);
    lv_anim_set_time(&fade_anim, 1000);
    lv_anim_set_exec_cb(&fade_anim, opa_anim_cb);
    lv_anim_set_path_cb(&fade_anim, lv_anim_path_ease_in_out);
    lv_anim_set_ready_cb(&fade_anim, welcome_fade_in_cb);
    lv_anim_start(&fade_anim);
}

// 清理开机界面
void cleanup_boot_screen(void) {
    // 清理定时器
    if(boot_timer) {
        lv_timer_del(boot_timer);
        boot_timer = NULL;
    }
    if(progress_timer) {
        lv_timer_del(progress_timer);
        progress_timer = NULL;
    }
    
    // 重置全局指针
    welcome_label = NULL;
    home_icon = NULL;
    progress_arc = NULL;
    loading_label = NULL;
    boot_screen = NULL;
    
    // 重置进度
    boot_progress = 0;
}
